package _2022.hot100._62_不同路径;

import java.util.ArrayList;
import java.util.List;

/**
 * @author： YHSimon
 * @date： 2022-04-15 10:37
 */
public class Solution {

    public int uniquePaths2(int m,int n){
        //定义
        int[][] dp=new int[m][n];
        //初始化
        for(int i=0;i<m;i++){
            dp[i][0]=1;
        }
        for(int j=0;j<n;j++){
            dp[0][j]=1;
        }
        //状态转移
        for(int i=1;i<m;i++){
            for(int j=1;j<n;j++){
                dp[i][j]=dp[i-1][j]+dp[i][j-1];
            }
        }
        return dp[m-1][n-1];
    }
    //超时
    public int uniquePaths(int m, int n) {
        List<Integer> res=new ArrayList<>();
        res.add(0);
        int[][] record=new int[m][n];
        int x=0,y=0;
        dfs(res,record,x,y,m,n);
        return res.get(0);
    }

    private void dfs(List<Integer> res,int[][] record,int x,int y,int m,int n) {
        if(x==m-1&&y==n-1){
            res.set(0, res.get(0)+1);
        }
        //向右
        if(y+1<n&&record[x][y+1]==0){
            record[x][y+1]=1;
            dfs(res,record, x, y+1, m, n);
            record[x][y+1]=0;
        }
        //向下
        if(x+1<m&&record[x+1][y]==0){
            record[x+1][y]=1;
            dfs(res, record,x+1,y , m, n);
            record[x+1][y]=0;
        }
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.uniquePaths2(3, 7));
    }
}
